package peer5

import "sync"

// 只支持 int 即可。

type MyMap struct {
	mu  sync.Mutex
	data map[interface{}]interface{}
}

func (m *MyMap) Load(key interface{}) (value interface{}, ok bool) {
	m.mu.Lock()
	value, ok = m.data[key]
	m.mu.Unlock()
	return
}

func (m *MyMap) Store(key, value interface{}) {
	m.mu.Lock()
	m.data[key] = value
	m.mu.Unlock()
}

func (m *MyMap) Delete(key interface{}) {
	//删除时，可能key正被访问，删除也要争抢锁
	m.mu.Lock()
	delete(m.data,key)
	m.mu.Unlock()
}

func (m *MyMap) LoadOrStore(key, value interface{}) (actual interface{}, loaded bool) {
	m.mu.Lock()
	actual, loaded = m.data[key]
	if !loaded{
		m.data[key] = value
		actual = value
	}
	m.mu.Unlock()
	return
}

func (m *MyMap) LoadAndDelete(key interface{}) (value interface{}, loaded bool) {
	m.mu.Lock()
	value, loaded = m.data[key]
	if loaded{
		delete(m.data, key)
	}
	m.mu.Unlock()
	return
}

func main2() {}
